'use strict';
/**
 * @description: 查询是否为会员
 */
exports.add = async (ctx) => {
  let { data: { name, type, json }, cloud } = ctx;
  try {

    let sql = `INSERT INTO agreement (gmt_create,gmt_modified,name,type,json) VALUES(now(),now(),?,?,?);`

    let agreement = await cloud.dataspace.executeSql(sql, [name, type, json ])
    return { success: true, insertId: agreement.insertId }
  } catch (e) { return { success: false, msg: e } }
};


exports.getAll= async (ctx) => {
  let { data: { page=1, size =10 }, cloud } = ctx;

  try {
    let sql2 = `select id,name,type,json from agreement`
    let agreement = await cloud.dataspace.executeSql(sql2, [])
    if (agreement) {
      return { success: true, data: {  arr: agreement } }
    } else {
      return { success: false, msg: "获取失败" }
    }
  } catch (e) { return { success: false, msg: e } }
};


exports.deleteOne = async (ctx) => {
  let { data: { id }, cloud } = ctx;

  try {
    let sql = `DELETE FROM agreement WHERE id = ${id}`;
    let agreement = await cloud.dataspace.executeSql(sql, []);
    if (agreement && agreement.affectedRows) {
      return { success: true }
    } else {
      return { success: false, msg: "删除失败" }
    }
  } catch (e) { return { success: false, msg: e } }
};


exports.updateOne = async (ctx) => {
  let { data: { id,json }, cloud } = ctx;

  try {
    let sql = `update agreement set json=${json} WHERE id = ${id}`;
    let agreement = await cloud.dataspace.executeSql(sql, []);
    if (agreement && agreement.affectedRows) {
      return { success: true }
    } else {
      return { success: false, msg: "删除失败" }
    }
  } catch (e) { return { success: false, msg: e } }
};

